<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Msg;
use DB;

class MsgController extends Controller
{   
    public function index(){
        //显示所有留言
        /*$msgs = DB::table('msgs')->get();
        return view('msg.index',['msgs'=>$msgs]);*/
        $msgs = Msg::all();
        // $msgs = Msg::where('id','>',2)->get();
        // $msgs = Msg::all('title','content');
        // $msgs = Msg::all(['title','content','email']);
        // $msgs = Msg::where('id','>',3)->get(['content','email']);
        // $msgs = Msg::where('id','>',4)->select('title','content','email')->get();
        // $msgs = Msg::where('id','>',2)->select(['title','content','email'])->get();
        // $msgs = Msg::where('id','>',2)->orderBy('id','desc')->get();
        // $msgs = Msg::where('id','>',2)->orderBy('id','desc')->skip(2)->take(2)->get();
        // $msgs = Msg::where('id','>',1)->orderBy('id','desc')->skip(2)->limit(3)->get();
        $msg = ['count'=>Msg::count()];
        return view('msg.index',['msgs'=>$msgs,'msg'=>$msg]);
    }

    public function add(Request $req){
        //展示模板并添加方法
        if(!$_POST){
            return view('msg.add'); 
        }else{
            $msg = new \App\Msg();
            $path = str_random(6).'.png';
            $req->file('photo')->move(public_path('/image/'),$path);
            $msg->title = $_POST['title'];
            $msg->content = $_POST['content'];
            $msg->email = $_POST['email'];
            $msg->pic = $req->input('pic','/public/image/'.$path);
            // $data = ['title'=>$_POST['title'],'content'=>$_POST['content'],'email'=>$_POST['email']];
            
            return $msg->save() ? redirect('msg/add') : 'fail';
        }
    }

  /*  public function addPost(){
        // 添加方法
        $rs = DB::table('msgs')->insert(['title'=>$_POST['title'],'content'=>$_POST['content'],'email'=>$_POST['email']]);
        return $rs ? redirect('msg/add') : 'fail';
        $msg = new \App\Msg();
        $msg->title = $_POST['title'];
        $msg->content = $_POST['content'];
        $msg->email = $_POST['email'];
        return $msg->save() ? redirect('msg/add') : 'fail';
    }
*/
    public function del($id){
        //删除方法和跳转
        /*if(DB::table('msgs')->where('id',$id)->delete()){
            return redirect('Msg/index');
        }else{
            return 'fail';
        }*/
        if(Msg::find($id)->delete()){
            return redirect('msg/index');
        }else{
            return 'fail';
        }
    }

    public function up(Request $req,$id){
        //获取数据并修改
        /*if(empty($_POST)){
            $row = DB::table('msgs')->where('id',$id)->first();
            return view('msg.up',['row'=>$row]);
        }else{
            $row = ['title'=>$_POST['title'],'content'=>$_POST['content'],'email'=>$_POST['email']];
            if(DB::table('msgs')->where('id',$id)->update($row)){
                return redirect('msg/index');
            }else{
                echo 'fail';
            }
        }*/
        if(empty($_POST)){
            $msg = Msg::find($id);
            return view('msg.up',['msg'=>$msg]);
        }else{
            print_r($req->all());
            $msg = Msg::find($id);
            // $msg->title = $_POST['title'];
            // $msg->content = $_POST['content'];
            // $msg->email = $_POST['email'];
            // 直接访问属性
            $msg->title = $req->title;
            $msg->content = $req->content;
            $msg->email = $req->email;
            $msg->pubtime = $req->input('pubtime',time());
            return $msg->save() ? redirect('msg/index') : 'fail';
        }
    }
    
}